/*var carselect = new Array();
var storeselect = new Array();
var bmselect = new Array();
var userselect = new Array();
$(function () {
    initThisApp();
})
function initThisApp() {
    // storeselect = [];
    // carselect = [];
    // bmselect = [];
    // userselect = [];
    $.ajax({
        url: "./goPCDD/initDisPage", //请求的url地址
        xhrFields: {
            withCredentials: true
        },
        async:false,
        dataType: "json", //返回格式为json
        type: "GET", //请求方式
        processData: false, //对表单data数据是否进行序列化
        contentType: false, //dataType设置你收到服务器数据的格式
        headers: {
            'Content-Type': 'application/json'
        },
        beforeSend: function() {
            //请求前的处理
        },
        success: function(data) {
            if(data.status==0){
                // top.location.href = "../../login.html";
                window.location.href="./login.html";
            }else{
                var a = JSON.parse(data.data);
                var carnoarr = a.data;
                carselect.splice(0,carselect.length);
                for(var i=0;i<carnoarr.length;i++){
                    var temp = {
                        value: carnoarr[i].CarNo,
                        label: carnoarr[i].CarNo
                    };
                    carselect.push(temp)

                }
                console.log(carselect)
                var storearr = a.data1;
                storearr = storearr.distinct();
                storeselect.splice(0,storeselect.length);
                for(var i=0;i<storearr.length;i++){
                    var temp = {
                        value: storearr[i].Store,
                        label: storearr[i].Store
                    };
                    storeselect.push(temp)
                }
            }
        },
        complete: function() {
            //请求完成的处理
        },
        error: function() {}
    });
}*/
Array.prototype.distinct = function(){
    var arr = this,
        result = [],
        i,
        j,
        len = arr.length;
    for(i = 0; i < len; i++){
        for(j = i + 1; j < len; j++){
            if(arr[i].Store.toString() === arr[j].Store.toString()){
                j = ++i;
            }
        }
        result.push(arr[i]);
    }
    return result;
}

Array.prototype.distinct1 = function(){
    var arr = this,
        result = [],
        i,
        j,
        len = arr.length;
    for(i = 0; i < len; i++){
        for(j = i + 1; j < len; j++){
            if(arr[i].Bm.toString() === arr[j].Bm.toString()){
                j = ++i;
            }
        }
        result.push(arr[i]);
    }
    return result;
}

Array.prototype.distinct2 = function(){
    var arr = this,
        result = [],
        i,
        j,
        len = arr.length;
    for(i = 0; i < len; i++){
        for(j = i + 1; j < len; j++){
            if(arr[i].UserNo.toString() === arr[j].UserNo.toString()){
                j = ++i;
            }
        }
        result.push(arr[i]);
    }
    return result;
}

var pageSize = 99999999;
var vm = new Vue({
    el: "#app",
    data:function(){
        return{
            YueMan:"",
            Status:"",
            StartDate:"",
            EndDate:"",
            StatusList: [
                {
                    value: '',
                    label: '全部'
                },
                {
                    value: '1',
                    label: '已约车'
                },
                {
                    value: '2',
                    label: '已派车'
                },
                {
                    value: '3',
                    label: '已返回'
                },
                {
                    value: '4',
                    label: '约车撤销'
                }
            ],
            //派车弹框Start
            PaiID:'',
            CarSelect:'',
            StoreSelect:'',
            BmSelect:'',
            UserSelect:'',
            PaiModel:false,
            PaiModelItem:{
                Store:'',
                Bm:'',
                UserNo:'',
                Name:'',
                CarNo:'',
                PaiIsOrNotPin:''
            },
            //派车弹框End
            //撤销派车弹框Start
            PaiCheID:'',
            PaiCheMan:'',
            PaiCheCarNo:'',
            PaiCheItem:{
                PaiCheReason:''
            },
            PaiCheModel:false,
            //撤销派车弹框End
            //搜索结果
            columns1: [
                {
                    type: 'index',
                    title: '序号',
                    align: 'center',
                },
                {
                    title: '单据编号',
                    key: 'DocNo',
                    align: 'center',
                },
                /*{
                    title: '调度类型',
                    key: 'Status',
                    align: 'center',
                    render:(h,params)=> {
                        var Status = params.row.Status;
                        if(Status=="1"){
                            Status = '已约车';
                        }
                        if(Status=="2"){
                            Status = '已派车';
                        }
                        if(Status=="3"){
                            Status = '已返回';
                        }
                        return h('span',Status);
                    }
                },*/
                {
                    title: '约车人',
                    key: 'YueMan',
                    align: 'center',
                },
                {
                    title: '中心店面',
                    key: 'YueStore',
                    align: 'center',
                },
                {
                    title: '约车人部门',
                    key: 'YueBm',
                    align: 'center',
                },
                {
                    title: '目的地',
                    key: 'ArrivePlace',
                    align: 'center',
                },
                {
                    title: '调度类型',
                    key: 'Status',
                    align: 'center',
                    render:(h,params)=> {
                        var Status = params.row.Status;
                        if(Status=="1"){
                            Status = '已约车';
                        }
                        if(Status=="2"){
                            Status = '已派车';
                        }
                        if(Status=="3"){
                            Status = '已返回';
                        }
                        if(Status=="4"){
                            Status = '约车撤销';
                        }
                        return h('span',{
                            style:{
                                color: (Status=='已约车')?"green":(Status=='已派车')?"red":(Status=='已返回')?"black":"blue"
                            }
                        },Status)
                    }
                },
                {
                    title: '预计出发时间',
                    key: 'YueStartDate',
                    align: 'center',
                },
                {
                    title: '预计返回时间',
                    key: 'YueEndDate',
                    align: 'center',
                },
                {
                    title: '返回时间',
                    key: 'FanDate',
                    align: 'center',
                },
                {
                    title: '约车事由',
                    key: 'YueReason',
                    align: 'center',
                },
                {
                    title: '同行人数',
                    key: 'YueManNumber',
                    align: 'center',
                },
                {
                    title: '随车物品',
                    key: 'YueGoods',
                    align: 'center',
                },
                {
                    title: '约车特殊情况说明',
                    key: 'YueSpecialExplain',
                    align: 'center',
                },
                {
                    title: '约车撤销原因',
                    key: 'YueCheReason',
                    align: 'center',
                },
                {
                    title: '派车撤销原因',
                    key: 'PaiCheReason',
                    align: 'center',
                },
            ],
            columns2: [
                {
                    type: 'selection',
                    align: 'center',
                    width:52,
                },
                {
                    title: '单据编号',
                    key: 'DocNo',
                    align: 'center',
                    width:115
                },
                {
                    title: '约车人',
                    key: 'YueMan',
                    align: 'center',
                    width:70
                },
                {
                    title: '目的地',
                    key: 'ArrivePlace',
                    align: 'center',
                },
                {
                    title: '预计出发时间',
                    key: 'YueStartDate',
                    align: 'center',
                },
                {
                    title: '预计返回时间',
                    key: 'YueEndDate',
                    align: 'center',
                },
                {
                    title: '同行人数',
                    key: 'YueManNumber',
                    align: 'center',
                    width:75
                },
            ],
            // 接下来绑定数据，分别对应前面的列的key值来进行数据绑定
            data1: [],
            data2:[],
            pageSize2:5,
            currentPageSize2:1,
            selectYueS:[],
            oldYue:[],
            // 初始化信息总条数
            dataCount:0,
            dataCount2:0,
            stripe:true
        }
    },
    created(){
        var self = this;
        $.ajax({
            url: "./goPCDD/initDisPage", //请求的url地址
            xhrFields: {
                withCredentials: true
            },
            async:false,
            dataType: "json", //返回格式为json
            type: "GET", //请求方式
            processData: false, //对表单data数据是否进行序列化
            contentType: false, //dataType设置你收到服务器数据的格式
            headers: {
                'Content-Type': 'application/json'
            },
            beforeSend: function() {
                //请求前的处理
            },
            success: function(data) {
                if(data.status==0){
                    // top.location.href = "../../login.html";
                    window.location.href="./login.html";
                }else{
                    var a = JSON.parse(data.data);
                    var carnoarr = a.data;
                    self.CarSelect='';
                    self.CarSelect = carnoarr;
                    var storearr = a.data1;
                    storearr = storearr.distinct();
                    self.StoreSelect='';
                    self.StoreSelect = storearr;
                }
            },
            complete: function() {
                //请求完成的处理
            },
            error: function() {}
        });
    },
    methods: {
        ok () {
        },
        cancel () {
        },
        handleRowChange(currentRow, oldCurrentRow){
            console.log(currentRow)
            console.log(oldCurrentRow)
        },
        goPaiModel(){
            this.PaiModel = true;
        },
        changeBm(){
            this.BmSelect = null;
            this.PaiModelItem.Bm = '';
            var Store = this.PaiModelItem.Store;
            if(!Store){
                return null;
            }
            var jsondata = {"Store":Store};
            var data_json = JSON.stringify(jsondata)
            var self = this;
            $.ajax({
                url: "./goPCDD/queryBmByStore", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    if(data.status==1){
                        self.BmSelect = '';
                        var a = JSON.parse(data.data);
                        var bmarr = a.data;
                        bmarr = bmarr.distinct1();
                        self.BmSelect = bmarr;
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });

        },
        changeUser(){
            this.UserSelect = '';
            this.PaiModelItem.UserNo = '';
            this.PaiModelItem.Name = '';
            var Store = this.PaiModelItem.Store;
            var Bm = this.PaiModelItem.Bm;
            if(!Store||!Bm){
                return null;
            }
            var jsondata = {"Store":Store,"Bm":Bm};
            var data_json = JSON.stringify(jsondata)
            var self = this;
            $.ajax({
                url: "./goPCDD/queryUserByStoreAndBm", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    if(data.status==1){
                        self.UserSelect = '';
                        var a = JSON.parse(data.data);
                        var userarr = a.data;
                        userarr = userarr.distinct2();
                        self.UserSelect=userarr
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });

        },
        rowClic(a,index){
            this.$refs.selection.toggleSelect(index);
        },
        //8 移除选中人员
        removeYue(index){
            var yue = this.selectYueS[index];
            this.selectYueS.splice(index,1);
            for (var i in  this.data2){
                if (this.data2[i].DocNo==yue.DocNo){
                    this.rowClic(this.data2,Number(i));
                    break;
                }
            }
        },
        //9 点击框变化时
        SeleChange(selection){
            //取消
            if (this.oldYue.length-selection.length>=0){
                for (var i=0; i<this.oldYue.length; i++){
                    var isAdd = true;
                    for (var j=0; j<selection.length; j++){
                        if (this.oldYue[i].gh == selection[j].gh){
                            isAdd=false;
                        }
                    }
                    if (isAdd){
                        for (var k=0; k<this.selectYueS.length; k++){
                            if (this.selectYueS[k].DocNo==this.oldYue[i].DocNo){
                                this.selectYueS.splice(k,1);
                                break;
                            }
                        }
                    }
                }
            }
            //增加
            if (this.oldYue.length-selection.length<0){
                for (var i=0; i<selection.length; i++){
                    var isAdd = true;
                    for(var k=0; k<this.selectYueS.length; k++){
                        if (selection[i].DocNo==this.selectYueS[k].DocNo){
                            isAdd=false;
                            break;
                        }
                    }
                    if (isAdd){
                        this.selectYueS.push(selection[i]);
                    }
                }
            }
            if (selection.length==0){
                this.oldYue=[];
            }else{
                this.oldYue=selection;
            }
            //人数
            if (this.selectYueS&&this.selectYueS.length==0){
                this.selectYueS = selection;
            }

        },
        getYue(){
            var info ;
            var total;
            var jsondata={"page":this.currentPageSize2, "rows":5};
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goPCDD/queryAllYueDis", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    info = eval(data.data);
                    total = data.totalnum;
                },
                error: function() {},
            });
            if(info){
                this.data2 = info;
                this.dataCount2 = total;
            }
        },
        reset(){
            this.YueMan = "";
            this.Status = "";
            this.StartDate = "";
            this.EndDate = "";
        },
        querytable(pageNum,pageSize){
            var info ;
            var total;
            var YueMan = this.YueMan;
            var Status = this.Status;
            var StartDate = this.StartDate;
            var EndDate = this.EndDate;
            if(StartDate==''||StartDate==null){
                StartDate = '';
            }else{
                var tempStartDate = new Date(StartDate);
                StartDate = tempStartDate.toLocaleDateString();
            }
            if(EndDate==''||EndDate==null){
                EndDate = '';
            }else{
                var tempEndDate = new Date(EndDate);
                EndDate = tempEndDate.toLocaleDateString();
            }

            var jsondata={"page":pageNum, "row":pageSize,"YueMan":YueMan,"Status":Status,"StartDate":StartDate,"EndDate":EndDate};
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goPCDD/queryAllDoc", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    info = eval(data.data);
                    total = data.totalnum;
                },
                error: function() {},
            });
            if(info){
                this.data1 = info;
                this.dataCount = total;
            }
        },
        // 2 分页
        changepage(index){
            this.querytable(index,pageSize);
        },
        changepage2(index){
            this.currentPageSize2 = index;
            this.getYue();
        },
        //判断是否为面积
        isMianJi(mj){
            var reg = /^[1-9]+\d*(\.\d{0,2})?$|^0?\.\d{0,2}$/;
            if(reg.test(mj)){
                return true;
            }else{
                return false;
            }
        },

        saveZWInfo(){
            var self = this;
            var zwmj = self.zwItem.StandArea;
            var zwbh = self.zwItem.StandNo.trim();
            var zwmc = self.zwItem.StandName.trim();
            if(zwbh==''||zwbh==null){
                toastr.warning("展位编号不得为空");
                return false;
            }
            if(zwmc==''||zwmc==null){
                toastr.warning("展位名称不得为空");
                return false;
            }
            if(this.isMianJi(zwmj)){

            }else{
                toastr.warning("展位面积请填写两位小数以内的正数");
                return false;
            }
            var jsondata = self.zwItem;
            var data_json = JSON.stringify(jsondata)
            $.ajax({
                url: "./goSHGL/saveZWInfo", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.status=='1'){
                        toastr.success('保存成功');
                    }else{
                        toastr.error('保存失败，请重新录入');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        //3 修改
        edit(params){
            var self = this
            self.editDataModalFlag = true
            self.categoreObject = params.row;
        },
        //撤销派车为已约车状态
        delPaiInfo(params){
            var self = this;
            var EditNameStr = parent.$("#uname1").text();
            var PaiCheMan = EditNameStr;
            var ID = this.PaiCheID;
            var PaiCarNo = this.PaiCheCarNo;
            var PaiMan = this.PaiCheMan;
            var PaiCheReason = this.PaiCheItem.PaiCheReason;
            var jsondata = {"ID":ID,"PaiCheReason":PaiCheReason,"PaiCheMan":PaiCheMan,"PaiMan":PaiMan,"PaiCarNo":PaiCarNo};
            var data_json = JSON.stringify(jsondata)
            $.ajax({
                url: "./goPCDD/delPaiInfo", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    if(data.status=="1"){
                        self.PaiCheItem.PaiCheReason = '';
                        toastr.success('撤销派车成功');
                        self.PaiModelItem.UserNo = '';
                        self.PaiModelItem.Name = '';
                        self.PaiModelItem.CarNo = '';
                        self.PaiModelItem.Store = '';
                        self.PaiModelItem.Bm = '';
                        self.PaiModelItem.PaiIsOrNotPin = '';
                    }else{
                        toastr.error('撤销失败，请重新撤销');
                    }
                },
                error: function() {
                    toastr.error('删除失败，请刷新页面');
                },
            });
            this.querytable("1","99999999999");
        },

        SavePaiInfo(params){
            var selectYueS = this.selectYueS;
            var self = this;
            var EditNameStr = parent.$("#uname1").text();
            var InputMan1 = EditNameStr;
            var PaiMan = this.PaiModelItem.UserNo;
            if(selectYueS.length<=0){
                toastr.warning("请选择约车单据")
                return null;
            }
            if(PaiMan==''||PaiMan==null){
                toastr.warning("请选择司机姓名")
                return null;
            }
            var PaiCarNo = this.PaiModelItem.CarNo;
            if(PaiCarNo ==''||PaiCarNo ==null){
                toastr.warning("请选择车牌号")
                return null;
            }
            var Pin = 0;
            if(selectYueS.length>1){
              Pin = 1;
            }
            var jsondata = {"selectYueS":selectYueS,"PaiMan":PaiMan,"PaiCarNo":PaiCarNo,"InputMan1":InputMan1,"PaiIsOrNotPin":Pin};
            var data_json = JSON.stringify(jsondata)
            $.ajax({
                url: "./goPCDD/SavePaiInfo", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    if(data.status=="1"){
                        var index ;
                        var tempSelect = self.CarSelect;
                        for(var i=0;i<tempSelect.length;i++){
                            if(tempSelect[i].CarNo.toString()==self.PaiModelItem.CarNo.toString()){
                                index = i;
                            }
                        }
                        tempSelect.splice(index,1);
                        self.CarSelect='';
                        self.CarSelect=tempSelect;
                        self.PaiModelItem.UserNo = '';
                        self.PaiModelItem.Name = '';
                        self.PaiModelItem.CarNo = '';
                        self.PaiModelItem.Store = '';
                        self.PaiModelItem.Bm = '';
                        self.PaiModelItem.PaiIsOrNotPin = '';
                        self.currentPageSize2 = 1;
                        self.selectYueS = [];
                        self.oldYue = [];
                        self.dataCount2 = 0;
                        self.data2 = [];
                        toastr.success('派车成功');
                    }else{
                        toastr.error('派车失败，请重新派车');
                    }
                },
                error: function() {
                    toastr.error('派车失败，请刷新页面');
                },
            });
            this.querytable("1","99999999999");
        },
        /*上传查看资料 开始*/
        // 上传授权书前的事件钩子
        handleUpload (file) {
            var self = this;
            var ID = this.SHID;
            // 选择文件后 这里判断文件类型 保存文件 自定义一个keyid 值 方便后面删除操作
            if(this.file.length>=1){
                this.$Notice.warning({
                    title: '警告',
                    desc: '只允许上传一张图片'
                });
                return false;
            }
            // 保存文件到总展示文件数据里
            this.file.push(file);
            // 保存文件到需要上传的文件数组里
            this.uploadFile.push(file)
            //预览
            var reader = new FileReader();
            reader.readAsDataURL(file);
            this.format=['jpg','jpeg','png'];
            // reader.onload = function () {
            //     self.src = reader.result;
            // }
            setTimeout(() => {
                if (!self.src){
                    self.src=reader.result;
                }
            }, 40);
            // 返回 falsa 停止自动上传 我们需要手动上传
            return false;
        },
        //上传执照
        handleUpload1 (file1) {
            var self = this;
            var ID = this.SHID;
            // 选择文件后 这里判断文件类型 保存文件 自定义一个keyid 值 方便后面删除操作
            if(this.file1.length>=1){
                this.$Notice.warning({
                    title: '警告',
                    desc: '只允许上传一张图片'
                });
                return false;
            }
            // 保存文件到总展示文件数据里
            this.file1.push(file1);
            // 保存文件到需要上传的文件数组里
            this.uploadFile1.push(file1)
            //预览
            var reader = new FileReader();
            reader.readAsDataURL(file1);
            this.format=['jpg','jpeg','png'];
            // reader.onload = function () {
            //     self.src1 = reader.result;
            // }
            setTimeout(() => {
                if (!self.src1){
                    self.src1=reader.result;
                }
            }, 100);
            // 返回 falsa 停止自动上传 我们需要手动上传
            return false;
        },
        //上传身份证
        handleUpload2 (file2) {
            var self = this;
            var ID = this.SHID;
            // 选择文件后 这里判断文件类型 保存文件 自定义一个keyid 值 方便后面删除操作
            if(this.file2.length>=1){
                this.$Notice.warning({
                    title: '警告',
                    desc: '只允许上传一张图片'
                });
                return false;
            }
            // 保存文件到总展示文件数据里
            this.file2.push(file2);
            // 保存文件到需要上传的文件数组里
            this.uploadFile2.push(file2)
            //预览
            var reader = new FileReader();
            reader.readAsDataURL(file2);
            this.format=['jpg','jpeg','png'];
            // reader.onload = function () {
            //     self.src2 = reader.result;
            // }
            setTimeout(() => {
                if (!self.src2){
                    self.src2=reader.result;
                }
            }, 40);
            // 返回 falsa 停止自动上传 我们需要手动上传
            return false;
        },

        delectFile (keyID) { // 删除文件
            // 删除总展示文件里的当前文件
            this.file.splice(0);
            // 删除需要上传文件数据里的当前文件
            this.uploadFile.splice(0);
            this.src=null;
        },
        delectFile1 (keyID) { // 删除文件
            // 删除总展示文件里的当前文件
            this.file1.splice(0);
            // 删除需要上传文件数据里的当前文件
            this.uploadFile1.splice(0);
            this.src1=null;
        },
        delectFile2 (keyID) { // 删除文件
            // 删除总展示文件里的当前文件
            this.file2.splice(0);
            // 删除需要上传文件数据里的当前文件
            this.uploadFile2.splice(0);
            this.src2=null;
        },

        upload () { // 上传文件
            for (let i = 0; i < this.uploadFile.length; i++) {
                let item = this.file[i];
                this.$refs.upload.post(item);
            }
        },
        upload1 () { // 上传文件
            for (let i = 0; i < this.uploadFile1.length; i++) {
                let item = this.file1[i];
                this.$refs.upload1.post(item);
            }
        },
        upload2 () { // 上传文件
            for (let i = 0; i < this.uploadFile2.length; i++) {
                let item = this.file2[i]
                this.$refs.upload2.post(item);
            }
        },
        uploadSuccess(){
            toastr.success("上传授权书成功")
        },
        uploadSuccess1(){
            toastr.success("上传执照成功")
        },
        uploadSuccess2(){
            toastr.success("上传身份证成功")
        },
        /*上传查看资料结束*/
        /*查看资料*/
        goLookInfo(params){
            var ID = params.row.ID;
            window.location.href="./car_look.html?CarID="+ID;
        }
        /*查看资料结束*/

    }
})